package com.cyf.plc.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.cyf.plc.R;
import com.cyf.plc.databinding.ItemRoomDarkBinding;
import com.cyf.plc.databinding.ItemRoomLightBinding;
import com.cyf.plc.model.PlcRoom;
import com.cyf.plc.widget.SelectLineTextView;

import java.util.List;

/**
 * Author：Lei on 2020/12/13
 */
public class RoomDarkAdapter extends RecyclerView.Adapter<RoomDarkAdapter.ViewHolder>{

    List<PlcRoom> roomList;
    Context context;
    OnItemClickListener listener;
    private int selectPosition = 0;

    public RoomDarkAdapter(List<PlcRoom> roomList, Context context, OnItemClickListener listener) {
        this.roomList = roomList;
        this.context = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRoomDarkBinding roomLightBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_room_dark, parent, false);
        return new ViewHolder(roomLightBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PlcRoom plcRoom = roomList.get(position);
        plcRoom.setRoomSelect(position == selectPosition);
        holder.binding.setRoom(plcRoom);
        holder.tvSelectRoom.lineTvText(plcRoom.getRoomName());
        holder.tvSelectRoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectPosition = position;
                notifyDataSetChanged();
                listener.onItemClicked(selectPosition);
            }
        });
    }

    @Override
    public int getItemCount() {
        return roomList == null ? 0:roomList.size();
    }

    public int getSelectPosition(){
        return selectPosition;
    }

    public interface OnItemClickListener {
        void onItemClicked(int selectPosition);
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        ItemRoomDarkBinding binding;
        SelectLineTextView tvSelectRoom;

        public ViewHolder(@NonNull ItemRoomDarkBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            this.tvSelectRoom = binding.tvSelectRoom;
        }
    }
}
